package dk.simonvogensen.itodo.server;

//import dk.simonvogensen.itodo.server.Task.Support.CoreSupport.ByteBuffer;

import dk.simonvogensen.itodo.shared.model.TodoItem;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.logging.Logger;
//import java.util.*;

/**
 * DataPingServlet allows you to test your HTTP POST/GET operations. Whatever data you send it, is sent back, so you
 * can see progress monitors working in reporting Send/Recieve updates.
 *
 * @author Nazmul Idris
 * @version 1.0
 * @since Mar 28, 2008, 7:06:21 PM
 */
public class AndroidServlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(AndroidServlet.class.getName());

    private TodoService service = new TodoService(PMF.get());

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
        String index = request.getParameter("index");
        log.info("androidservlet.index="+index);
        TodoItem item;
        if(index == null) {
            item = service.getLatestTodoItem();
        } else {
            item = service.getTodoItem(Integer.parseInt(index));
        }

        res.setContentType("text/html;charset=UTF-8");
        ServletOutputStream sos = res.getOutputStream();
        String content = item.getIndex()+System.getProperty("line.separator")+item.getText();
        res.setContentLength(content.length());
        sos.write(content.getBytes("UTF-8"));

        sos.flush();
        sos.close();
    }


    /*
    protected void doPost2(HttpServletRequest request, HttpServletResponse res)
        throws ServletException, IOException
    {

      ByteBuffer inputBB = new ByteBuffer(request.getInputStream());
      ByteBuffer outputBB = null;

      try {

        // extract the hashmap
        System.out.println("trying to extract hashtable from request");

        ObjectInputStream ois = new ObjectInputStream(inputBB.getInputStream());
        Hashtable<String, String> input = (Hashtable<String, String>) ois.readObject();
        System.out.println("got the uid/pwd from the client:" + input);

        Object retval = _processInput(input);
        System.out.println("created response hashtable, sending it back");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(retval);

        outputBB = new ByteBuffer(baos.toByteArray());

        System.out.println("sent response back...");

      }
      catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
      }

      ServletOutputStream sos = res.getOutputStream();

      if (outputBB != null) {
        res.setContentType("application/octet-stream");
        res.setContentLength(outputBB.getSize());
        sos.write(outputBB.getBytes());
      }
      else {
        res.setContentType("application/octet-stream");
        res.setContentLength(inputBB.getSize());
        sos.write(inputBB.getBytes());
      }

      sos.flush();
      sos.close();

    }
    */
/** actually get user profile object from service and send it back */
/*
private Hashtable<DataKeys, Serializable> _processInput(Hashtable<String, String> input) {

  Hashtable<DataKeys, Serializable> retval = new Hashtable<DataKeys, Serializable>();

  retval.put(DataKeys.str1, input.get("uid"));
  retval.put(DataKeys.str2, input.get("pwd"));

  try {
    InputStream is = getClass().getClassLoader().getResourceAsStream("information.png");
    if (is == null) System.out.println("is is null!");
    ByteBuffer bb = new ByteBuffer(is);
    retval.put(DataKeys.img1, bb.getBytes());
  }
  catch (IOException e) {
    System.out.println(e);
    e.printStackTrace();
  }

  return retval;

}
*/
}//end class AndroidServlet
